package com.hr.reactive.flux;

import com.hr.reactive.common.Utils;
import com.hr.reactive.mono.s01.sub.SubscriberImpl;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author huran
 */
public class Lec03FluxVsList {
    public static void main(String[] args) {
//        List<String> namesList = getNamesList(10);
//        System.out.println(namesList);

//        getNamesFlux(10).subscribe(Utils.subscriber());
        var s = new SubscriberImpl();
        getNamesFlux(10).subscribe(s);
        s.getSubscription().request(3);
        s.getSubscription().cancel();
    }

    public static List<String> getNamesList(int count) {
        return IntStream.rangeClosed(1, count)
                .mapToObj(i -> getName()).collect(Collectors.toList());
    }

    public static Flux<String> getNamesFlux(int count) {
        return Flux.range(1, count)
                .map(i -> getName());
    }

    private static String getName() {
        Utils.sleepSeconds(1000);
        return Utils.faker().name().firstName();
    }
}
